package com.atguigu.eduteacher.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduteacher.javabean.EduTeacher;
import com.atguigu.eduteacher.javabean.dto.TeacherQuery;
import com.atguigu.eduteacher.service.EduTeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-10-10
 */
@RestController
@RequestMapping("/eduservice/teacher")
@RequiredArgsConstructor
@CrossOrigin
public class EduTeacherController {

    private final EduTeacherService teacherService;

//    获取全部讲师
    @GetMapping("findAll")
    public R list() {
        return R.ok().data("teachers", teacherService.list(null));
    }

    @DeleteMapping("/{id}")
    public R removeTeacherById(@PathVariable String id) {
        if (teacherService.removeById(id)) {
            return R.ok().data("result", "删除成功");
        }
        return R.error().data("result", "删除失败");
    }


    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit) {
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        teacherService.page(pageParam, null);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "分页条件查询讲师列表")
    @PostMapping("pageTeacherCondition/{page}/{limit}")
//    http://localhost:8001/eduteacher/teacher/pageTeacherCondition/1/10
    public R pageListWithConditions(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "teacherQuery", value = "查询条件", required = true)
            @RequestBody TeacherQuery teacherQuery) {

        Page<EduTeacher> pageParam = new Page<>(page, limit);

        IPage<EduTeacher> teachers =
                teacherService.pageTeacherConditions(pageParam, teacherQuery);
        List<EduTeacher> records = teachers.getRecords();
        long total = teachers.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping
    public R save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        teacherService.save(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id,
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id) {
        EduTeacher teacher = teacherService.getById(id);
        return R.ok().data("item", teacher);
    }
}

